﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManageSystem
{
    public partial class AddBook : Form
    {

        private int mode;
        private string time;
        private bool dateIsChange = false;
        private string bookType;
        public AddBook()
        {
            InitializeComponent();
        }

        public AddBook(int mode)
        {
            InitializeComponent();
            this.mode = mode;
        }

        public AddBook(int mode, string bookId, string bookName, string bookAuthor, string bookPub, string bookContent, string bookType, string bookTime)
        {
            InitializeComponent();
            this.mode = mode;
            textBox1.Text = bookId;
            textBox1.ReadOnly = true;
            textBox2.Text = bookName;
            textBox3.Text = bookAuthor;
            textBox4.Text = bookPub;
            textBox5.Text = bookContent;

            this.bookType = bookType;
            //this.time = bookTime;
            dateTimePicker1.Value = Convert.ToDateTime(bookTime);
        }

        private void AddBook_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“typeNameSet.bookType”中。您可以根据需要移动或删除它。
            this.bookTypeTableAdapter.Fill(this.typeNameSet.bookType);

            comboBox1.Text = bookType;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入图书编号！");
                return;
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("请输入书名！");
                return;
            }
            else if (textBox3.Text == "")
            {
                MessageBox.Show("请输入图书作者！");
                return;
            }
            else if (textBox4.Text == "")
            {
                MessageBox.Show("请输入图书出版社！");
                return;
            }
            else if (!IsNumberic(textBox1.Text))
            {

                MessageBox.Show("图书编号请输入数字！");

                this.textBox2.Focus();
                return;
            }

            if (mode == 0)
            {

                string commend = "select * from bookInfo where bookId='" + this.textBox1.Text + "'";
                SqlDataReader dataReader = Utils.getDataReader(commend);
                if (dataReader.Read())
                {
                    MessageBox.Show("该图书编号已存在，请修改图书编号!");
                    return;
                }

                string str = "insert bookInfo(bookId,bookName,bookAuthor,bookPub,bookContent,bookType,bookTime) values(" + textBox1.Text + ",N'" + textBox2.Text + "',N'" + textBox3.Text + "',N'" + textBox4.Text + "',N'" + textBox5.Text + "',N'" + comboBox1.Text + "','" + dateTimePicker1.Text + "')";
                if (Utils.ExecuteNoQuery(str) != -1)
                {
                    MessageBox.Show("添加成功");
                    ManageBook a = (ManageBook)this.Owner;
                    a.initData();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else if (mode == 1)
            {

                if (dateIsChange)
                    time = dateTimePicker1.Text;

                string str = "update bookInfo set bookName=N'" + textBox2.Text + "',bookAuthor=N'" + textBox3.Text + "',bookPub=N'" + textBox4.Text + "',bookContent=N'" + textBox5.Text + "',bookType=N'" + comboBox1.Text + "',bookTime='" + time + "' where bookId=" + textBox1.Text;
                if (Utils.ExecuteNoQuery(str) != -1)
                {
                    MessageBox.Show("修改成功");
                    ManageBook a = (ManageBook)this.Owner;
                    a.initData();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
        }

        private bool IsNumberic(string oText)
        {
            try
            {
                int var1 = Convert.ToInt32(oText);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateIsChange = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
